package kr.hkit.androidcomplete01.ch19;

import kr.hkit.androidcomplete01.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class HandlerEx extends Activity {
	int mMainValue;
	int mBackValue;
	TextView mMainTv;
	TextView mBackTv;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_threadtest);
		mMainTv = (TextView) findViewById(R.id.mainvalue);
		mBackTv = (TextView) findViewById(R.id.backvalue);
		
		BackThread thread = new BackThread();
		thread.setDaemon(true); //종료되면 스레드도 같이 종료
		thread.start();
	}
	public void mOnClick(View v){
		mMainValue++;
		mMainTv.setText("Main Value : " + mMainValue);
		mBackTv.setText("BackValue : " + mBackValue);
	}
	
	
	class BackThread extends Thread{
		public void run() {
			while(true){
				mBackValue++;
//				mBackTv.setText("BackValue : " + mBackValue); 실행시 오류.. 문법상 오류가 없지만
				mHandler.sendEmptyMessage(0);
				try{ Thread.sleep(1000);}catch(InterruptedException e){;}
			}
		}
	}
	
	Handler mHandler = new Handler(){
		public void handleMessage(Message msg){
			if(msg.what == 0){
				mBackTv.setText("BackValue : " + mBackValue);
			}
		}
	};
}
